'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:ManageRepayCtrl
 * @description
 * # ManageRepayCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('ManageRepayCtrl', function($scope, $rootScope, $filter, openApi, $http, Util, $location, $routeParams, FormSubmit) {
        $scope.sheetObj = {};
        // 底部关联模块类型选框添加
        var cerReference = {},
            selectData, params;
        // 选择材料证明
        var cerReference = {},
            selectData, params;
        $scope.showcerList = function() {
            // 获取证明列表
            if (!$scope.param.performanceApplyId) {
                Util.Toast("请选择工程名称");
                return;
            }
            var param = {};
            param.url = openApi.performanceListA;
            param.params = {
                id: $scope.param.performanceApplyId
            };
            Util.getPerformanceList(param, function(data) {
                $scope.performanceList = data.result
            })

        };
        $scope.chooseProList = function() {
            cerReference = {
                name: "ManageRepayCtrl",
                selectTab: "performanceReturn_select_performance",
                selectTabContent: "工程名称"
            };
            params = $scope.param;
            Util.savaParamForReview(params, $routeParams.id);
            Util.savaDataforsearch(cerReference);

            $location.path("finance/choosepeople")
        };
        var selectLists = [];
        $scope.toggle = function(item) {
            var curItemIndex;
            curItemIndex = $scope.checkIfExsit(item);
            if (curItemIndex == -1) {
                selectLists.push(item)
            } else {
                selectLists.splice(curItemIndex, 1)
            }
            $scope.param.detailsStr = getNames();
        };
        var getNames = function() {
            var arr = [];
            if (selectLists.length > 0) {
                selectLists.forEach(function(item, index) {
                    arr.push(item.perName)
                })
            }
            return arr.join();

        };

        $scope.checkIfExsit = function(item) {
            var selecrIndex = -1;
            var pkId;
            pkId = item.pkId;
            if (selectLists.length < 1) {
                return selecrIndex
            }
            selectLists.forEach(function(item, index) {
                if (item.pkId == pkId) {
                    selecrIndex = index;
                    return selecrIndex;
                }
            });
            return selecrIndex;
        };

        var getDetails = function() {
            var detailsArr = []
            selectLists.forEach(function(item, index) {
                detailsArr.push(item.pkId)
            });
            return detailsArr;
        };
        $scope.submit = function() {
            var detailsArr;
            // 获取已经选择的列表
            detailsArr = getDetails();
            if (!$scope.param.remark) {
                Util.Toast("请填写备注");
                return;
            }
            if (!$scope.param.projectName) {
                Util.Toast("请填写工程名称");
                return;
            }
            if (detailsArr.length < 1 && !$scope.param.perIds) { //如果是回显的数据会有perids的
                Util.Toast("请填选择证明材料");
                return;
            }
            $scope.param.perIds = detailsArr.join() || $scope.param.perIds;
            // 提交
            var urlParam = "?operationId=-2" + "&&processId=" + $routeParams.id + "&&url=" + openApi.performanceReturn
            var options = {};
            options.urlParam = urlParam;
            options.param = $scope.param;
            var TVtype;
            TVtype = Util.getTVType(); //根据页面的来源来判断是发起的流程还是修改的流程，如果是发起的则不管，如果是修改的则获取数据用来回显
            // 如果是修改页面的话则提交的地址不一样，参数一样
            delete options.param.seals
            if (TVtype == Util.UPDATE) {
                options.url = openApi.ip + openApi.performanceReturn
            }
            new FormSubmit(options);
        };
        var init = function() {
            // 获取回显的详细信息
            Util.getDetailData($routeParams.id, function(data) {
                $scope.param = data;
            });
            // 显示审批记录判断
            Util.showloglist($routeParams.id, function(data) {
                $scope.logData = data;
            });

        };
        // 初始化
        init();
    });
